package com.hnichr.ump.salon.service.impl;

import com.google.zxing.WriterException;
import com.hnichr.ump.common.Constant;
import com.hnichr.ump.common.salon.entity.SalonInfo;
import com.hnichr.ump.common.salon.service.SalonInfoService;
import com.hnichr.ump.common.service.QRCodeService;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class SalonInfoServiceImpl extends BaseServiceImpl<SalonInfo> implements SalonInfoService {
    @Value("${ump.h5Url}")
    private String h5Url;

    @Value("${minio.resUrl}")
    private String resUrl;

    @Value("${salon.applyPage}")
    private String applyPage;

    @Value("${salon.signInPage}")
    private String signInPage;

    @Autowired
    private QRCodeService qRCodeService;

    @Transactional
    public List<SalonInfo> list(Criteria c){
        List<SalonInfo> salonInfoList = this.list(SalonInfo.class,c);
        for(SalonInfo salonInfo:salonInfoList){
            fullData(salonInfo);
        }
        return salonInfoList;
    }

    @Transactional
    public Page<SalonInfo> listPage(Pageable page, Criteria c){
        Page<SalonInfo> pageList = this.listPage(SalonInfo.class,page,c);
        for(SalonInfo salonInfo:pageList.getContent()){
            fullData(salonInfo);
        }
        return pageList;
    }

    @Transactional
    public SalonInfo getById(String id){
        SalonInfo salonInfo = this.getById(SalonInfo.class,id);
        if(salonInfo==null){
            throw new MBootException("找不到会议信息");
        }
        fullData(salonInfo);
        return salonInfo;
    }



    @Transactional
    public SalonInfo add(SalonInfo salonInfo){

        if(salonInfo.getStartTime().compareTo(salonInfo.getEndTime())>=0){
            throw new MBootException("开始时间不能晚于结束时间");
        }

        this.save(salonInfo);
        String applyUrl = h5Url + "/" + applyPage+"?type=signUp"+"&id="+salonInfo.getId();
        String signInUrl = h5Url + "/" + applyPage+"?type=signIn"+"&id="+salonInfo.getId();
        String applyQrcodeUrl;
        String signInQrcodeUrl;
        try{
            applyQrcodeUrl =  qRCodeService.generateSimpleQrcode(applyUrl,500,500);
            signInQrcodeUrl = qRCodeService.generateSimpleQrcode(signInUrl,500,500);
        }
        catch (IOException|WriterException e) {
            throw new MBootException("生成二维码失败");
        }
        salonInfo.setApplyUrl(applyUrl);
        salonInfo.setSignInUrl(signInUrl);
        salonInfo.setApplyQrCode(applyQrcodeUrl);
        salonInfo.setSignInQrCode(signInQrcodeUrl);
        this.update(salonInfo);
        return salonInfo;
    }
    @Transactional
    public void remove(String ids){
        String[] aId = ids.split(",");
        for(String id:aId){
            this.remove(SalonInfo.class,id);
        }
    }

    private void fullData(SalonInfo salonInfo){
        if(new Date().compareTo(salonInfo.getEndTime())>0){
            salonInfo.setStatus("1");
            salonInfo.setStatusDesc("已结束");
        }
        else{
            salonInfo.setStatus("0");
            salonInfo.setStatusDesc("报名中");
        }

        String meetingTime = DateFormatUtils.format(salonInfo.getStartTime(),"yyyy年MM月dd日 HH点mm分")+"至"+DateFormatUtils.format(salonInfo.getEndTime(),"yyyy年MM月dd日 HH点mm分");
        salonInfo.setMeetingTime(meetingTime);

    }
}
